package org.awei.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.awei.dao.IStuDao;
import org.awei.model.Student;
import org.awei.service.IStuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/my")
@Slf4j
public class MyController {
    @Autowired
    private IStuDao stuDao;

    @RequestMapping("/hello")
    @ResponseBody
    public String main(){
        return "hello";
    }

    @RequestMapping("/test")
    public JSONObject test(){
        JSONObject json = new JSONObject();
        json.put("code",1);
        json.put("msg","success");
        return json;
    }


    @RequestMapping("/t3")
    public ModelAndView t3(){
        ModelAndView mv = new ModelAndView();

        Student s = new Student();
        s.setId(13);
        s.setMajorName("张三");
        mv.addObject("obj",s);

        List<String> list = new ArrayList<>();
        list.add("first");
        list.add("second");
        list.add("third");
        list.add("four");

        mv.addObject("list",list);
        mv.addObject("no",11);
        mv.addObject("str","abc");
        mv.addObject("bool",true);

        mv.setViewName("demo");
        return mv;
    }
    @Autowired
    public IStuService studentService;

    @RequestMapping("index")
    public ModelAndView index(Student student,Integer pageNo){
        ModelAndView mv = new ModelAndView();

        mv.setViewName("index");
        return mv;
    }

    @RequestMapping("add")
    public ModelAndView add(){
        ModelAndView mv = new ModelAndView();

        mv.setViewName("add");
        return mv;
    }

    @RequestMapping("del")
    @ResponseBody
    public String del(Student student){
        System.out.println(student.getId());
        int count =stuDao.del(student);
        if(count>=1){
            return "1";
        }else{
            return "0";
        }
    }

    @RequestMapping("insert")
    @ResponseBody
    public String insert(Student student){
        int count =stuDao.insert(student);
        if(count>=1){
            return "1";
        }else{
            return "0";
        }
    }


    @Autowired
    private Jedis jedis;


    @RequestMapping("/test1")
    @ResponseBody
    public JSONObject test(HttpServletRequest req){
        String res = jedis.ping();
        System.out.println(res);
        jedis.setex("name1",10,"zahnsgxuguangshabi");

        JSONObject json = new JSONObject();
        json.put("code",1);
        json.put("msg","success");
        return json;
    }

    @GetMapping("test2")
    @ResponseBody
    public String test2(){
        log.debug("debug");
        log.info("info");
        log.warn("warn");
        log.error("error");
        return "张旭光肾虚";
    }
}


